package com.frxuexi.park2305.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import androidx.recyclerview.widget.LinearLayoutManager
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.mvi.BaseFragment
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2305.R
import com.frxuexi.park2305.databinding.FragmentHomeBinding
import com.frxuexi.park2305.notice.NoticeAdapter
import com.frxuexi.park2305.notice.NoticeIntent
import com.frxuexi.park2305.notice.NoticeViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding,NoticeIntent,NoticeViewModel>() {
    override val binding: FragmentHomeBinding by lazy { FragmentHomeBinding.inflate(layoutInflater) }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()
    val noticeAdapter by lazy { NoticeAdapter() }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val data = it.data as PagingData<NoticeBeanItem>
                        noticeAdapter.submitData(data.filter { it.title.isNotEmpty() })
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvNotice.apply {
            adapter=noticeAdapter
            layoutManager=LinearLayoutManager(requireContext())
        }
        vm.sendIntent(NoticeIntent.getNotices)

//        EventBus.getDefault().post(3)
    }

}